﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SQLite;
using System.IO;

namespace Scripts.RCPKit
{
    class GuildDataSQLite
    {
        public const string SQLiteGuildsFile = "GuildsDatabase.s3db";

        private static String  connString = "Data Source=" + SQLiteGuildsFile + ";Pooling=True;Max Pool Size=20;Cache Size=100";
        private static  SQLiteConnection sql_con;

        /// <summary>
        /// Get guild information by guild name.
        /// </summary>
        /// <param name="GuildName"></param>
        /// <returns></returns>
        public static Guild GetGuildByName(string GuildName)
        {
            int guildID;
            string guildName;
            string guildMOTD;
            string serializedguildMembers;
            List<GuildMember> guildMembers;

            using (sql_con = new SQLiteConnection(connString))
            {
                SQLiteCommand GuildCmd = new SQLiteCommand("SELECT GuildID, GuildName, GuildMOTD, GuildMembers FROM Guilds WHERE Guildname = @GuildName", sql_con);
                GuildCmd.Parameters.Add("@GuildName", System.Data.DbType.String).Value = GuildName;

                using (SQLiteDataReader dr = GuildCmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        guildID = (int)dr.GetValue(0);
                        guildName = (string)dr.GetValue(1);
                        guildMOTD = (string)dr.GetValue(2);
                        serializedguildMembers = (string)dr.GetValue(3);
                        guildMembers = Guild.DerializeMembers(serializedguildMembers);
                        return new Guild(guildID, guildName, guildMOTD, guildMembers);
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// Get guild information by GuildID
        /// </summary>
        /// <param name="GuildID"></param>
        /// <returns></returns>
        public static Guild GetGuildByID(int GuildID)
        {
            int guildID;
            string guildName;
            string guildMOTD;
            string serializedguildMembers;
            List<GuildMember> guildMembers;

            using (sql_con = new SQLiteConnection(connString))
            {
                SQLiteCommand GuildCmd = new SQLiteCommand("SELECT GuildID, GuildName, GuildMOTD, GuildMembers FROM Guilds WHERE GuildID = @GuildID", sql_con);
                GuildCmd.Parameters.Add("@GuildID", System.Data.DbType.Int32).Value = GuildID;

                using (SQLiteDataReader dr = GuildCmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        guildID = (int)dr.GetValue(0);
                        guildName = (string)dr.GetValue(1);
                        guildMOTD = (string)dr.GetValue(2);
                        serializedguildMembers = (string)dr.GetValue(3);
                        guildMembers = Guild.DerializeMembers(serializedguildMembers);
                        return new Guild(guildID, guildName, guildMOTD, guildMembers);
                    }
                }
            }
            return null;
        }

        public static string GetPlayersGuild(string PlayerName)
        {
            string guildName;
            using (sql_con = new SQLiteConnection(connString))
            {
                SQLiteCommand GuildCmd = new SQLiteCommand("SELECT ActorGuild FROM GuildMembers WHERE ActorName = @ActorName", sql_con);
                GuildCmd.Parameters.Add("@ActorName", System.Data.DbType.String).Value = PlayerName;

                using (SQLiteDataReader dr = GuildCmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        guildName = (string)dr.GetValue(1);
                        return guildName;
                    }
                }
            }
            return null;
        }

        //public static AddMember(string Name, string GuildName)
        //{
        
        //}
    }
}
